<template>
	<view>
		<view style="background: #007AFF;height: 400rpx;"></view>
		<view class="postfix">
			<view class="row-css" style="text-align: center;padding: 40rpx;justify-content: center;align-items: center;" @click="showup()">
				金额(元)
				<image style="width: 40rpx;height: 30rpx;" mode="aspectFill" :src="isshow == true ? '../../../static/icon_eye.png' : '../../../static/icon_eye_in.png'" />
			</view>
			<view style="text-align: center;font-size: 60rpx;margin-bottom: 60rpx;">
				<text v-if="isshow">{{ balance }}</text>
				<text v-else>****</text>
			</view>
			<view class="row-css" style="width: 100%;margin-top: 80rpx;">
				<button
					class="mini-btn"
					style="color: #007AFF;border: 1rpx solid #007AFF; margin-right: 20rpx;"
					type="primary"
					plain="true"
					size="default"
					@click="ontrandetails()"
				>
					明细
				</button>
				<button class="mini-btn" style="background: #007AFF;" type="primary" size="default" @click="onwithdraw()">提现</button>
			</view>
			<view style="text-align: center;margin-top: 40rpx;color: #007AFF;" @click="onbanklist">管理我的银行卡</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			isshow: false,
			balance: '',
			shipInfo: {}
		};
	},
	onLoad(options) {
		let title = options.pay == 1 ? '委托收款人钱包(已开通)' : '收款信息'
		uni.setNavigationBarTitle({
			title
		})
	},
	onShow() {
		if(uni.getStorageSync(this.DATA_KEY.shipAuthFlag)) {
			this.savePlatformDriverLive(this.orderNumber);
			uni.setStorageSync(this.DATA_KEY.shipAuthFlag, false);
		} else {
			this.getShipInfo()
		}
		this.showup()
	},
	methods: {
		showup() {
			if (!this.isshow) {
				this.wxRequest({
					url: `${this.API}/mini/jzb/queryBalanceCaptain`,
					method: 'POST',
					title: '余额查询中...',
					success: res => {
						this.isshow = !this.isshow;
						let num = (res.data.data.availAmount - res.data.data.payFreeze).toFixed(2)
						this.balance = num > 0 ? num : 0
					}
				});
			} else {
				this.isshow = !this.isshow;
			}
		},
		onwithdraw() {
			if (this.shipInfo.realNameStatus == '0') {
				let that = this
				uni.showModal({
					title: '认证提示',
					content: '承运人未活体，请先进行活体认证',
					success(result) {
						if (result.confirm) {
							that.goAuth()
						}
					}
				})
				return
			}
			
			let datapurse = {
				category: false,
				// payId: this.payId,
				isshow: this.isshow,
				balance: this.balance
			};
			uni.navigateTo({
				url: '/pages/home/withdrawal?datapurse=' + JSON.stringify(datapurse)
			});
		},
		ontrandetails() {
			uni.navigateTo({
				url: '/pages/home/billway'
			});
		},
		onbanklist() {
			uni.navigateTo({
				url: '/pages/home/bankcard'
			});
		},
		async goAuth() {
			const result = await this.getDriverIdCardInfo();
			this.accessToken = result.data.accessToken;
			const result2 = await this.getFaceQRCode(result.data);
			let orderNumber = result2.data.orderNumber;
			this.orderNumber = orderNumber;
			// const result3 = await this.savePlatformDriverLive(orderNumber)
			let that = this
			// #ifdef APP-PLUS
			plus.share.getServices(
				function(res) {
					uni.setStorageSync(that.DATA_KEY.shipAuthFlag, true);
					var sweixin = null;
					console.log(res);
					for (var i = 0; i < res.length; i++) {
						var t = res[i];
						console.log(t);
						if (t.id == 'weixin') {
							sweixin = t;
						}
					}
					if (sweixin) {
						console.log('dddd=', 'a=' + sweixin + '=b');
						sweixin.launchMiniProgram({
							// id: 'gh_cec8224b00da',
							id: 'gh_45eec401ff1c',
							type: 0, //0-正式版 1.测试版，2，体验版
							path: 'pages/verify/info/index?id=' + orderNumber
						});
					}
				},
				function(res) {
					console.log(JSON.stringify(res));
				}
			);
			// #endif
			
			// #ifdef MP-WEIXIN
			uni.navigateToMiniProgram({
				// appId: 'wxfd2861dc1c86b3a0',
				appId: 'wxfc1e27e05fdd062b',
				path: 'pages/verify/info/index?id=' + orderNumber,
				envVersion: 'release',
				success(res) {
					uni.setStorageSync(that.DATA_KEY.shipAuthFlag, true);
					console.log('跳转成功');
				},
				fail(res) {
					console.log('跳转失败' + JSON.stringify(res));
				}
			});
			// #endif
		},
		getDriverIdCardInfo() {
			return new Promise((resolve, reject) => {
				let that = this;
				that.wxRequest({
					url: `${that.API}/captainApp/getCaptainIdCardInfo`,
					method: 'GET',
					title: '加载中...',
					success: res => {
						resolve(res.data)
					},
					fail: err => {
						reject(err)
					}
				});
			})
		},
		getFaceQRCode(data) {
			return new Promise((resolve, reject) => {
				let that = this;
				that.wxRequestData({
					url: `${that.API2}/api/face/getFaceQRCode?access_token=` + data.accessToken,
					method: 'POST',
					data: {
						companyTaxNumber: data.taxNumber,
						realName: data.CaptainName,
						idCard: data.idcard,
						idPhotoUp: data.idcard1Base64,
						idPhotoDown: data.idcard2Base64,
						callbackUrl: `${that.API}/driverService/isLivingBodyState`
					},
					success: res => {
						resolve(res.data)
					},
					fail: err => {
						reject(err)
					}
				})
			})
		},
		savePlatformDriverLive(orderNumber) {
			return new Promise((resolve, reject) => {
				let that = this;
				that.wxRequest({
					url: `${that.API}/captainApp/savePlatformDriverLive`,
					method: 'POST',
					data: {
						captainId: that.shipInfo.captain.captainId, //船长id
						orderNumber: orderNumber, //活体编号
						accessToken: that.accessToken
					},
					success: res => {
						this.getShipInfo()
						resolve(res.data)
					},
					fail: err => {
						reject(err)
					}
				})
			})
		},
		getShipInfo() {
			this.wxRequest({
				url: `${this.API}/captainApp/captainAuthentication?etpId=${uni.getStorageSync(this.DATA_KEY.etpId)}`,
				method: 'GET',
				success: res => {
					if (res.data.code == 0) {
						this.shipInfo = res.data.data
					}
				}
			});
		}
	},
	onUnload() {
		uni.setStorageSync(this.DATA_KEY.shipAuthFlag, false)
	}
};
</script>

<style>
page {
	/* background: #ffffff; */
	font-size: 30rpx;
}

.row-css {
	display: flex;
	flex-direction: row;
}

.column-css {
	display: flex;
	flex-direction: column;
}

.postfix {
	position: fixed;
	background: #ffffff;
	border-radius: 10rpx;
	left: 80rpx;
	right: 80rpx;
	top: 120rpx;
	height: 400rpx;
}
.mini-btn {
	width: 100%;
	background: #e04b28;
	font-size: 28rpx;
	padding: 10rpx 0;
	margin: 0 20rpx;
}
</style>
